package edu.hcmiu.portal.eventhook;

import com.liferay.portal.PortalException;
import com.liferay.portal.SystemException;
import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
import com.liferay.portal.kernel.events.Action;
import com.liferay.portal.kernel.events.ActionException;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.servlet.HttpHeaders;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.util.PortalUtil;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

import edu.hcmiu.portal.model.hook_user;
import edu.hcmiu.portal.service.hook_userLocalServiceUtil;



public class AuthenticationListener extends Action {

    public void run(HttpServletRequest request, HttpServletResponse response) throws ActionException {
        try {
            HttpSession session = request.getSession();

            long userID = PortalUtil.getUserId(request);
            User user = UserLocalServiceUtil.getUserById(userID);
            String userScreenName = user.getScreenName();
            String userEmail = user.getEmailAddress();
            String sessionId = session.getId();
            String remoteAddr = request.getRemoteAddr();
            String remoteHost = request.getRemoteHost();
            String userAgent = request.getHeader(HttpHeaders.USER_AGENT);
            DynamicQuery query = DynamicQueryFactoryUtil.forClass(hook_user.class)
                    .add(PropertyFactoryUtil.forName("userID").eq(new Long(userID)));
            List results = hook_userLocalServiceUtil.dynamicQuery(query);
            try {
                if (results.isEmpty()) {
                    hook_user userModel = hook_userLocalServiceUtil.createhook_user(userID);
                    userModel.setUserID(userID);
                    userModel.setUserName(userScreenName);
                    userModel.setUserEmail(userEmail);
                    hook_userLocalServiceUtil.addhook_user(userModel);
                } else {
                    System.out.println("This user is already exists");
                }
            } catch (SystemException ex) {
                Logger.getLogger(AuthenticationListener.class.getName()).log(Level.SEVERE, null, ex);
            }

            System.out.println("User ID:" + userID);
            System.out.println("User Screen Name:" + userScreenName);
            System.out.println("Session ID:" + sessionId);
            System.out.println("Remote Address:" + remoteAddr);
            System.out.println("Remote Host:" + remoteHost);
            System.out.println("user Agent:" + userAgent);
            JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
            jsonObj.put("userId", userID);
            jsonObj.put("sessionId", sessionId);
            jsonObj.put("remoteAddr", remoteAddr);
            jsonObj.put("remoteHost", remoteHost);
            jsonObj.put("userAgent", userAgent);

            session.setAttribute("USER_userinfo", jsonObj.toString());
        }
        catch (Exception e) {
			throw new ActionException(e);
        }
    }
        /* try {
            User userModel = PortalUtil.getUser(request);
            long userID = userModel.getUserId();
            System.out.println(userID);
            String screenName = userModel.getScreenName();
            String userEmail = userModel.getEmailAddress();

            DynamicQuery query = DynamicQueryFactoryUtil.forClass(hook_user.class)
                    .add(PropertyFactoryUtil.forName("userID").eq(userID));
            try {
                List resultUserList = hook_userLocalServiceUtil.dynamicQuery(query);
                if (resultUserList.isEmpty()) {
                    hook_user user = hook_userLocalServiceUtil.createhook_user(userID);
                    user.setUserID(userID);
                    user.setUserName(screenName);
                    user.setUserEmail(userEmail);
                    hook_userLocalServiceUtil.addhook_user(user);
                }
            } catch (SystemException ex) {
                Logger.getLogger(AuthenticationListener.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (PortalException ex) {
            Logger.getLogger(AuthenticationListener.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SystemException ex) {
            Logger.getLogger(AuthenticationListener.class.getName()).log(Level.SEVERE, null, ex);
        } */

}